<!DOCTYPE html>
<html lang="zh">
    <head>
  <!-- 元数据 -->
  <meta charset="utf-8">
  
  
  <title>Lua表 | 舛羽的笔记小站</title>
  <meta name="author" content="cy" />
  <meta http-equiv="Cache-Control" content="no-transform" />
  <meta http-equiv="Cache-Control" content="no-siteapp" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="robots" content="index,follow" />
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <meta name="format-detection" content="telphone=no, email=no" />
  
    <meta name="keywords" content="Lua" />
  
  <meta name="description" content="Lua中的表是非常重要的一个复杂变量，无论是数组还是之后需要实现的面向对象的类等，都是通过表来实现的。">
<meta property="og:type" content="article">
<meta property="og:title" content="Lua表">
<meta property="og:url" content="http://example.com/2023/07/11/Lua%E8%AF%AD%E6%B3%95/Lua%E8%A1%A8/index.html">
<meta property="og:site_name" content="舛羽的笔记小站">
<meta property="og:description" content="Lua中的表是非常重要的一个复杂变量，无论是数组还是之后需要实现的面向对象的类等，都是通过表来实现的。">
<meta property="og:locale">
<meta property="og:image" content="https://s2.loli.net/2023/07/11/32EBXSHzNc7RZCP.png">
<meta property="og:image" content="https://s2.loli.net/2023/07/11/moCO4wg5evXqTsR.png">
<meta property="og:image" content="https://s2.loli.net/2023/07/11/eiyWlpHjBOToF8L.png">
<meta property="og:image" content="https://s2.loli.net/2023/07/11/VZ4bjeX8QCAYm2z.png">
<meta property="og:image" content="https://s2.loli.net/2023/07/11/78naFViWBvHbuIY.png">
<meta property="og:image" content="https://s2.loli.net/2023/07/11/KkpzQE2FHsyg78R.png">
<meta property="og:image" content="https://s2.loli.net/2023/07/11/aDLnzv7WGFH1C8A.png">
<meta property="og:image" content="https://s2.loli.net/2023/07/11/tMWGcK8R7dHbjCP.png">
<meta property="og:image" content="https://s2.loli.net/2023/07/11/FwgWy1L5PTbHfIX.png">
<meta property="article:published_time" content="2023-07-10T18:18:04.000Z">
<meta property="article:modified_time" content="2023-07-11T15:22:40.955Z">
<meta property="article:author" content="cy">
<meta property="article:tag" content="Lua">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://s2.loli.net/2023/07/11/32EBXSHzNc7RZCP.png">
  
  <!-- 站点验证相关 -->
  
    
    
    
  
  <!-- 样式表文件 -->
  <link rel="stylesheet" id="kratos-css" href="/css/kratosr.min.css" media="all"></script>
  
    <link rel="stylesheet" id="darkmode-css" href="/css/kr-color-dark.min.css" media="(prefers-color-scheme: dark)"></script>
    <script src="/js/kr-dark.min.js"></script>
  
  
    <link rel="stylesheet" id="highlight-css" href="/css/highlight/night-eighties.min.css" media="all"></script>
  
  
  <link rel="stylesheet" id="fontawe-css" href="/vendors/font-awesome@4.7.0/css/font-awesome.min.css" media="all"></script>
  <link rel="stylesheet" id="nprogress-css" href="/vendors/nprogress@0.2.0/nprogress.css" media="all"></script>
  
  
  
    <link rel="stylesheet" href="/vendors/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css"></script>
  
  <!-- 不得不预先加载的一些JS文件 -->
  <script src="/vendors/jquery@3.6.0/dist/jquery.min.js"></script>
  
    <script src="/vendors/qrcode_js@1.0.0/qrcode.min.js"></script>
  
  
  <style>
    
      .kratos-cover.kratos-cover-2 {
        background-image: url('/images/banner.webp');
      }
    
    
      @media(min-width:768px) {
        body.custom-background {
          background-image: url('/images/bg.webp');
        }
      }
    
  </style>
  
<meta name="generator" content="Hexo 7.3.0"></head>


    <body class="custom-background">
        <div id="kratos-wrapper">
    <div id="kratos-page">
        <div id="kratos-header">
            <header id="kratos-desktop-topnav" class="kratos-topnav">
                <div class="container">
                    <div class="nav-header">
                        <nav id="kratos-menu-wrap">
                            <ul id="kratos-primary-menu" class="sf-menu">
                                
                                    
                                    
                                
                            </ul>
                        </nav>
                    </div>
                </div>
            </header>
            <header id="kratos-mobile-topnav" class="kratos-topnav">
                <div class="container">
                    <div class="color-logo"><a href="/">舛羽的笔记小站</a></div>
                    <div class="nav-toggle">
                        <a class="kratos-nav-toggle js-kratos-nav-toggle">
                            <i></i>
                        </a>
                    </div>
                </div>
            </header>
        </div>
        <div class="kratos-start kratos-hero-2">
            <!-- <div class="kratos-overlay"></div> -->
            <div class="kratos-cover kratos-cover-2 text-center">
                <div class="desc desc2 animate-box">
                    <a href="/">
                        <h2>舛羽的笔记小站</h2> <br />
                        <span></span>
                    </a>
                </div>
            </div>
        </div>

        <div id="kratos-blog-post">
            <div class="container">
                <div id="main" class="row">
                    

        

            <section class="col-md-8">

        

            <article itemscope itemtype="https://schema.org/Article">
    
    <link itemprop="mainEntityOfPage" href="http://example.com/2023/07/11/Lua%E8%AF%AD%E6%B3%95/Lua%E8%A1%A8/">
    <div class="kratos-hentry kratos-post-inner clearfix">
        <header class="kratos-entry-header">
            
                <h1 class="kratos-entry-title text-center" itemprop="name headline">Lua表</h1>
            
            
            <ul class="kratos-post-meta text-center">
                <li><time datetime="2023-07-10T18:18:04.000Z" itemprop="datePublished"><i class="fa fa-calendar"></i> 2023-07-11</time></li>
                <li itemprop="author" itemscope itemtype="https://schema.org/Person">
                    <i class="fa fa-user"></i> 作者 <span itemprop="name">cy</span>
                </li>
                <li>
                    <i class="fa fa-edit"></i> 
                    
                    
                        ~3.36K
                    
                    字
                </li>
                
            </ul>
        </header>
        <div class="kratos-post-content">
            
            <div id="expire-alert" class="alert alert-warning hidden" role="alert">
                <div class="icon"><i class="fa fa-warning"></i></div>
                <div class="text"><p>本文最后编辑于 <time datetime="1689088960955"></time> 前，其中的内容可能需要更新。</p></div>
            </div>
            
            
            
            <hr />
            <div itemprop="articleBody"><p>Lua中的表是非常重要的一个复杂变量，无论是数组还是之后需要实现的面向对象的类等，都是通过表来实现的。</p>
<span id="more"></span>
<h2 id="第三章-表"><a href="#第三章-表" class="headerlink" title="第三章 表"></a>第三章 表</h2><h3 id="3-1-一维数组"><a href="#3-1-一维数组" class="headerlink" title="3.1 一维数组"></a>3.1 一维数组</h3><p>一维数组是表最基础的用法，实现方式也非常简单，需要重点强调的一点是表实现的数组没有严格的类型限制，意味着<strong>任意类型的值都可以放入数组</strong>。同时需要强调的一点是<strong>数组的索引也是从1开始</strong>。事实上在lua语法中是没有数组这个概念的，所有的无论是一维还是二维数组，说到底就是表的运用罢了。</p>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">a=&#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="literal">nil</span>,<span class="number">5</span>,<span class="string">&quot;12345&quot;</span>,<span class="literal">nil</span>,<span class="literal">true</span>,<span class="number">6</span>&#125;;</span><br><span class="line"><span class="built_in">print</span>(a[<span class="number">0</span>]);</span><br><span class="line"><span class="built_in">print</span>(a[<span class="number">1</span>]);</span><br><span class="line"><span class="built_in">print</span>(a[<span class="number">5</span>]);</span><br><span class="line"><span class="built_in">print</span>(a[<span class="number">6</span>]);</span><br><span class="line"><span class="built_in">print</span>(a[<span class="number">7</span>]);</span><br><span class="line"><span class="built_in">print</span>(a[<span class="number">9</span>]);</span><br><span class="line"><span class="built_in">print</span>(a[<span class="number">10</span>]);</span><br></pre></td></tr></table></figure>
<p>输出的结果如下：</p>
<p><img src="https://s2.loli.net/2023/07/11/32EBXSHzNc7RZCP.png" alt=""></p>
<p>需要注意的是有些教程说nil值会截断数组，但我的测试并没有遇到这个问题，应该是版本有所更新，所以具体情况具体分析。</p>
<p>一维数组的遍历则是使用之前学到的循环的语法：</p>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">a=&#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="literal">nil</span>,<span class="number">5</span>,<span class="string">&quot;12345&quot;</span>,<span class="literal">nil</span>,<span class="literal">true</span>,<span class="number">6</span>&#125;;</span><br><span class="line"><span class="keyword">for</span> i=<span class="number">1</span>,#a <span class="keyword">do</span></span><br><span class="line">    <span class="built_in">print</span>(a[i]);</span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure>
<p>输出的结果如下图：</p>
<p><img src="https://s2.loli.net/2023/07/11/moCO4wg5evXqTsR.png" alt=""></p>
<h3 id="3-2-二维数组"><a href="#3-2-二维数组" class="headerlink" title="3.2 二维数组"></a>3.2 二维数组</h3><p>数组中的元素为一个数组时，这个数组就是二位数组。它的访问方式也和C#中的数组差不多。遍历方式也如出一辙。</p>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">a=&#123;&#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>&#125;,&#123;<span class="literal">nil</span>,<span class="string">&quot;54&quot;</span>&#125;&#125;;</span><br><span class="line"><span class="keyword">for</span> i=<span class="number">1</span>,#a <span class="keyword">do</span></span><br><span class="line">    <span class="keyword">for</span> j=<span class="number">1</span>,#a[i] <span class="keyword">do</span></span><br><span class="line">        <span class="built_in">print</span>(a[i][j]);</span><br><span class="line">    <span class="keyword">end</span></span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure>
<p>输出结果如下图：</p>
<p><img src="https://s2.loli.net/2023/07/11/eiyWlpHjBOToF8L.png" alt=""></p>
<h3 id="3-3-自定义索引"><a href="#3-3-自定义索引" class="headerlink" title="3.3 自定义索引"></a>3.3 自定义索引</h3><p>不同于数组的是表可以进行自定义索引，也就是由你自己对表中的数据进行索引定义。</p>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">F1=<span class="function"><span class="keyword">function</span><span class="params">()</span></span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;123&quot;</span>);</span><br><span class="line"><span class="keyword">end</span></span><br><span class="line">a=&#123;[<span class="number">0</span>]=<span class="number">1</span>,[<span class="number">3</span>]=<span class="string">&quot;123&quot;</span>,<span class="number">2</span>,<span class="number">8</span>,<span class="number">4</span>,[<span class="number">-1</span>]=<span class="string">&quot;Pokemon&quot;</span>,[<span class="string">&quot;Pokemon001&quot;</span>]=<span class="string">&quot;妙蛙种子&quot;</span>,[F1]=<span class="string">&quot;45&quot;</span>&#125;;</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;-1&quot;</span>..<span class="string">&quot; &quot;</span>..a[<span class="number">-1</span>]);</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;0&quot;</span>..<span class="string">&quot; &quot;</span>..a[<span class="number">0</span>]);</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;1&quot;</span>..<span class="string">&quot; &quot;</span>..a[<span class="number">1</span>]);</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;2&quot;</span>..<span class="string">&quot; &quot;</span>..a[<span class="number">2</span>]);</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;3&quot;</span>..<span class="string">&quot; &quot;</span>..a[<span class="number">3</span>]);</span><br><span class="line"><span class="comment">--print(&quot;4&quot;..&quot; &quot;..a[4]);</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;Pokemon001&quot;</span>..<span class="string">&quot; &quot;</span>..a[<span class="string">&quot;Pokemon001&quot;</span>]);</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;F1&quot;</span>..<span class="string">&quot; &quot;</span>..a[F1]);</span><br></pre></td></tr></table></figure>
<p>输出结果如下图：</p>
<p><img src="https://s2.loli.net/2023/07/11/VZ4bjeX8QCAYm2z.png" alt=""></p>
<p>首先我们可以发现自定,义索引中的索引可以是任何形式，无论是number还是string，甚至连function都可以当作索引。同时对于没有自定义的索引会依照顺序进行自动分配索引，并且这种自动分配的索引的优先级是高于自定义索引的。</p>
<p><strong>重点</strong>：此处有个究极深坑，如果自定义索引中间相差一个数，那么这个数会被赋值为nil，数组的长度会加上这个数继续计算，如果相差两个数，即两个nil黏在一起，那么数组会直接从这里截断，虽然直接输出可以输出后面的数，但是数组长度就不会计算后面的数了。讲起来可能不太好懂，我们直接看代码：</p>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">a=&#123;[<span class="number">1</span>]=<span class="number">1</span>,[<span class="number">2</span>]=<span class="number">2</span>,[<span class="number">4</span>]=<span class="number">4</span>,[<span class="number">6</span>]=<span class="number">6</span>&#125;;</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;length&quot;</span>..<span class="string">&quot;   &quot;</span>..#a);</span><br><span class="line"><span class="keyword">for</span> i=<span class="number">1</span>,#a <span class="keyword">do</span></span><br><span class="line">    <span class="built_in">print</span>(a[i]);</span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure>
<p>输出结果如下图：</p>
<p><img src="https://s2.loli.net/2023/07/11/78naFViWBvHbuIY.png" alt=""></p>
<p>然后看接下来这个例子，我们把4换成5</p>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">a=&#123;[<span class="number">1</span>]=<span class="number">1</span>,[<span class="number">2</span>]=<span class="number">2</span>,[<span class="number">5</span>]=<span class="number">4</span>,[<span class="number">6</span>]=<span class="number">6</span>&#125;;</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;length&quot;</span>..<span class="string">&quot;   &quot;</span>..#a);</span><br><span class="line"><span class="keyword">for</span> i=<span class="number">1</span>,#a <span class="keyword">do</span></span><br><span class="line">    <span class="built_in">print</span>(a[i]);</span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure>
<p>输出结果如下图：</p>
<p><img src="https://s2.loli.net/2023/07/11/KkpzQE2FHsyg78R.png" alt=""></p>
<p>这个坑非常影响使用，需要记住，不过我们平常也不会使用到直接用数字作为自定义索引，而是把它作为字典来使用。</p>
<h3 id="3-4-迭代器遍历"><a href="#3-4-迭代器遍历" class="headerlink" title="3.4 迭代器遍历"></a>3.4 迭代器遍历</h3><p>因为之前的自定义索引，我们可以看到使用表的长度来实现遍历并不好，因为可能会被截断，所以我们一般也不会使用长度来遍历表。</p>
<p>接下来使用类似于C++Stl中定义的迭代器来进行遍历。</p>
<p>从表的结构可以看出，表其实可以看成由一系列的键值对，可以使用lua中的ipairs方法和pairs来实现遍历。</p>
<h4 id="3-4-1-ipairs"><a href="#3-4-1-ipairs" class="headerlink" title="3.4.1 ipairs"></a>3.4.1 ipairs</h4><p>ipairs遍历还是从1开始往后遍历的，小于等于0的值得不到。</p>
<p>同时ipairs只能找到连续索引的<strong>键</strong>如果中间断序了，它也无法遍历出后面的内容。所以事实上ipairs也没啥用。</p>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">a=&#123;[<span class="number">0</span>]=<span class="number">0</span>,[<span class="number">1</span>]=<span class="number">1</span>,[<span class="number">-1</span>]=<span class="number">-1</span>,[<span class="number">2</span>]=<span class="number">2</span>,[<span class="number">4</span>]=<span class="number">4</span>,[<span class="number">6</span>]=<span class="number">6</span>&#125;;</span><br><span class="line"><span class="keyword">for</span> i,j <span class="keyword">in</span> <span class="built_in">ipairs</span>(a) <span class="keyword">do</span></span><br><span class="line">    <span class="built_in">print</span>(i..<span class="string">&quot;   &quot;</span>..j);</span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure>
<p>输出结果如下图：</p>
<p><img src="https://s2.loli.net/2023/07/11/aDLnzv7WGFH1C8A.png" alt=""></p>
<h4 id="3-4-2-pairs"><a href="#3-4-2-pairs" class="headerlink" title="3.4.2 pairs"></a>3.4.2 pairs</h4><p>pairs遍历就非常的智能，能够把所有的键都找到，通过键可以得到值。</p>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">print</span>(<span class="string">&quot;-------------pairs遍历键值-------------&quot;</span>)</span><br><span class="line">a=&#123;[<span class="number">0</span>]=<span class="number">0</span>,[<span class="number">1</span>]=<span class="number">1</span>,[<span class="number">-1</span>]=<span class="number">-1</span>,[<span class="number">2</span>]=<span class="number">2</span>,[<span class="number">4</span>]=<span class="number">4</span>,[<span class="number">6</span>]=<span class="number">6</span>&#125;;</span><br><span class="line"><span class="keyword">for</span> i,j <span class="keyword">in</span> <span class="built_in">pairs</span>(a) <span class="keyword">do</span></span><br><span class="line">    <span class="built_in">print</span>(i..<span class="string">&quot;   &quot;</span>..j);</span><br><span class="line"><span class="keyword">end</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;-------------pairs遍历键值-------------&quot;</span>)</span><br><span class="line">a=&#123;[<span class="number">0</span>]=<span class="number">0</span>,[<span class="number">1</span>]=<span class="number">1</span>,[<span class="number">-1</span>]=<span class="number">-1</span>,[<span class="number">2</span>]=<span class="number">2</span>,[<span class="number">4</span>]=<span class="number">4</span>,[<span class="number">6</span>]=<span class="number">6</span>&#125;;</span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">pairs</span>(a) <span class="keyword">do</span></span><br><span class="line">    <span class="built_in">print</span>(i);</span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure>
<p>输出结果如下图：</p>
<p><img src="https://s2.loli.net/2023/07/11/tMWGcK8R7dHbjCP.png" alt=""></p>
<h3 id="3-5-字典"><a href="#3-5-字典" class="headerlink" title="3.5 字典"></a>3.5 字典</h3><p>之前我提到过表的自定义索引最主要的作用还是用来模拟字典，因为无论什么变量类型都可以作为键，那么也可以模拟字典。</p>
<p>字典的声明和定义其实就是声明表，并使用自定义索引。</p>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">print</span>(<span class="string">&quot;-------------表模拟字典-------------&quot;</span>)</span><br><span class="line">a=&#123;[<span class="string">&quot;Fairy&quot;</span>]=<span class="string">&quot;仙子伊布&quot;</span>,[<span class="string">&quot;Water&quot;</span>]=<span class="string">&quot;火伊布&quot;</span>,[<span class="string">&quot;Dark&quot;</span>]=<span class="string">&quot;月亮伊布&quot;</span>&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">--输出方式</span></span><br><span class="line"><span class="built_in">print</span>(a[<span class="string">&quot;Fairy&quot;</span>]);</span><br><span class="line"><span class="built_in">print</span>(a.Fairy);</span><br><span class="line"><span class="comment">--修改方式</span></span><br><span class="line">a[<span class="string">&quot;Water&quot;</span>]=<span class="string">&quot;水伊布&quot;</span>;</span><br><span class="line">a.Water=<span class="string">&quot;水伊布&quot;</span>;</span><br><span class="line"><span class="built_in">print</span>(a.Water);</span><br><span class="line"><span class="comment">--新增方式</span></span><br><span class="line">a[<span class="string">&quot;Grass&quot;</span>]=<span class="string">&quot;叶伊布&quot;</span>;</span><br><span class="line">a.Grass=<span class="string">&quot;叶伊布&quot;</span>;</span><br><span class="line"><span class="built_in">print</span>(a.Grass);</span><br><span class="line"><span class="comment">--删除方式</span></span><br><span class="line">a[<span class="string">&quot;Dark&quot;</span>]=<span class="literal">nil</span>;</span><br><span class="line">a.Dark=<span class="literal">nil</span>;</span><br><span class="line"><span class="built_in">print</span>(a.Dark);</span><br><span class="line"></span><br><span class="line"><span class="comment">--遍历方式</span></span><br><span class="line"><span class="keyword">for</span> i,j <span class="keyword">in</span> <span class="built_in">pairs</span>(a) <span class="keyword">do</span></span><br><span class="line">    <span class="built_in">print</span>(i..<span class="string">&quot;  &quot;</span>..j);</span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure>
<p>输出结果如下图：</p>
<p><img src="https://s2.loli.net/2023/07/11/FwgWy1L5PTbHfIX.png" alt=""></p>
</div>
        </div>
        
            <div class="kratos-copyright text-center clearfix">
                <h5 itemprop="copyrightNotice">本作品采用 <a rel="license nofollow" target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/">知识共享署名-相同方式共享 4.0 国际许可协议</a> 进行许可</h5>
            </div>
        
        <footer class="kratos-entry-footer clearfix">
            
                <div class="post-like-donate text-center clearfix" id="post-like-donate">
                
                
                    <a class="share" href="javascript:;"><i class="fa fa-share-alt"></i> 分享</a>
                    <div class="share-wrap" style="display: none;">
    <div class="share-group">
        <a href="javascript:;" class="share-plain qq" onclick="share('qq');" rel="nofollow">
            <div class="icon-wrap">
                <i class="fa fa-qq"></i>
            </div>
        </a>
        <a href="javascript:;" class="share-plain qzone" onclick="share('qzone');" rel="nofollow">
            <div class="icon-wrap">
                <i class="fa fa-star"></i>
            </div>
        </a>
        <a href="javascript:;" class="share-plain weixin pop style-plain" rel="nofollow">
            <div class="icon-wrap">
                <i class="fa fa-weixin"></i>
            </div>
            <div class="share-int">
                <div class="qrcode" id="wechat-qr"></div>
                <p>打开微信“扫一扫”，打开网页后点击屏幕右上角分享按钮</p>
            </div>
        </a>
        <a href="javascript:;" class="share-plain weibo" onclick="share('weibo');" rel="nofollow">
            <div class="icon-wrap">
                <i class="fa fa-weibo"></i>
            </div>
        </a>
        <a href="javascript:;" class="share-plain facebook style-plain" onclick="share('facebook');" rel="nofollow">
            <div class="icon-wrap">
                <i class="fa fa-facebook"></i>
            </div>
        </a>
        <a href="javascript:;" class="share-plain twitter style-plain" onclick="share('twitter');" rel="nofollow">
            <div class="icon-wrap">
                <i class="fa fa-twitter"></i>
            </div>
        </a>
    </div>
    <script type="text/javascript">
        $(()=>{
            new QRCode("wechat-qr", {
                text: "http://example.com/2023/07/11/Lua%E8%AF%AD%E6%B3%95/Lua%E8%A1%A8/",
                width: 150,
                height: 150,
                correctLevel : QRCode.CorrectLevel.H
            });
        });
        function share(dest) {
            const qqBase        = "https://connect.qq.com/widget/shareqq/index.html?";
            const weiboBase     = "https://service.weibo.com/share/share.php?";
            const qzoneBase     = "https://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?";
            const facebookBase  = "https://www.facebook.com/sharer/sharer.php?";
            const twitterBase   = "https://twitter.com/intent/tweet?";
            const hostUrl       = "http://example.com/2023/07/11/Lua%E8%AF%AD%E6%B3%95/Lua%E8%A1%A8/";
            const title         = "「Lua表」";
            const excerpt       = `Lua中的表是非常重要的一个复杂变量，无论是数组还是之后需要实现的面向对象的类等，都是通过表来实现的。`;
            let _URL;
            switch (dest) {
                case "qq"       : _URL = qqBase+"url="+hostUrl+"&title="+title+"&desc=&summary="+excerpt+"&site=cxpy";     break;
                case "weibo"    : _URL = weiboBase+"url="+hostUrl+"&title="+title+excerpt;                                 break;
                case "qzone"    : _URL = qzoneBase+"url="+hostUrl+"&title="+title+"&desc=&summary="+excerpt+"&site=cxpy";  break;
                case "facebook" : _URL = facebookBase+"u="+hostUrl;                                                        break;
                case "twitter"  : _URL = twitterBase+"text="+title+excerpt+"&url="+hostUrl;                                break;
            }
            window.open(_URL);
        };
    </script>
</div>
                
                </div>
            
            <div class="footer-tag clearfix">
                <div class="pull-left">
                <i class="fa fa-tags"></i>
                    <a class="tag-none-link" href="/tags/Lua/" rel="tag">Lua</a>
                </div>
                <div class="pull-date">
                    <time datetime="2023-07-11T15:22:40.955Z" itemprop="dateModified">最后编辑：2023-07-11</time>
                </div>
            </div>
        </footer>
    </div>
    
        <nav class="navigation post-navigation clearfix" role="navigation">
            
            <div class="nav-previous clearfix">
                <a title=" Lua函数" href="/2023/07/10/Lua语法/Lua函数/">&lt; 上一篇</a>
            </div>
            
            
            <div class="nav-next clearfix">
                <a title=" Lua表实现类及表的公共操作" href="/2023/07/11/Lua语法/Lua表实现类及表的公共操作/">下一篇 &gt;</a>
            </div>
            
        </nav>
    
    
</article>

        

            </section>

        

                
            

<section id="kratos-widget-area" class="col-md-4 hidden-xs hidden-sm">
    <!-- 文章和页面根据splitter来分割，没有的话就从头开始设置为sticky -->
    
    
                <aside id="krw-about" class="widget widget-kratos-about clearfix">
    <div class="photo-background"></div>
    <div class="photo-wrapper clearfix">
        <div class="photo-wrapper-tip text-center">
            <img class="about-photo" src="/images/avatar.webp" loading="lazy" decoding="auto" />
        </div>
    </div>
    <div class="textwidget">
        <p class="text-center"></p>
    </div>
    <div class="site-meta">
        <a class="meta-item" href="/archives/">
            <span class="title">
                文章
            </span>
            <span class="count">
                39
            </span>
        </a>
        <a class="meta-item" href="/categories/">
            <span class="title">
                分类
            </span>
            <span class="count">
                7
            </span>
        </a>
        <a class="meta-item" href="/tags/">
            <span class="title">
                标签
            </span>
            <span class="count">
                10
            </span>
        </a>
    </div>
</aside>
            
                    <div class="sticky-area">
                
                
  <aside id="krw-categories" class="widget widget-kratos-categories clearfix">
    <h4 class="widget-title"><i class="fa fa-folder"></i>分类目录</h4>
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/C-%E7%9F%A5%E8%AF%86/">C#知识</a><span class="category-list-count">9</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Lua/">Lua</a><span class="category-list-count">10</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/Unity%E7%BD%91%E7%BB%9C%E6%B8%B8%E6%88%8F%E5%BC%80%E5%8F%91/">Unity网络游戏开发</a><span class="category-list-count">3</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%AE%9D%E5%8F%AF%E6%A2%A6%E5%B0%8F%E8%AF%B4/">宝可梦小说</a><span class="category-list-count">5</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E7%AC%94%E8%AE%B0/">操作系统笔记</a><span class="category-list-count">10</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E7%AC%94%E8%AE%B0/">数据结构笔记</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E9%9A%8F%E6%83%B3/">随想</a><span class="category-list-count">1</span></li></ul>
  </aside>


            
                
  <aside id="krw-tags" class="widget widget-kratos-tags clearfix">
    <h4 class="widget-title"><i class="fa fa-tags"></i>标签聚合</h4>
      <div class="tag-clouds">
        <a href="/tags/C/" style="font-size: 0.75em;">C#</a> <a href="/tags/Lua/" style="font-size: 0.8em;">Lua</a> <a href="/tags/unity/" style="font-size: 0.65em;">unity</a> <a href="/tags/%E5%AE%9D%E5%8F%AF%E6%A2%A6/" style="font-size: 0.7em;">宝可梦</a> <a href="/tags/%E5%BD%B1%E8%AF%84/" style="font-size: 0.6em;">影评</a> <a href="/tags/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/" style="font-size: 0.8em;">操作系统</a> <a href="/tags/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/" style="font-size: 0.6em;">数据结构</a> <a href="/tags/%E7%AE%97%E6%B3%95/" style="font-size: 0.6em;">算法</a> <a href="/tags/%E7%BD%91%E7%BB%9C%E6%B8%B8%E6%88%8F/" style="font-size: 0.65em;">网络游戏</a> <a href="/tags/%E9%9A%8F%E6%83%B3/" style="font-size: 0.6em;">随想</a>
      </div>
  </aside>

            
                
  <aside id="krw-posts" class="widget widget-kratos-posts">
  <h4 class="widget-title"><i class="fa fa-file"></i>最新文章</h4>
  <div class="tab-content">
      <ul class="list-group">
        
        
          
          
            <a class="list-group-item" href="/2025/10/08/%E9%9A%8F%E6%83%B3/%E3%80%8A%E6%98%9F%E4%B8%89%E5%9B%BD%E3%80%8B%E5%90%90%E6%A7%BD/"><i class="fa  fa-book"></i> 《星三国》吐槽</a>
            
          
        
          
          
            <a class="list-group-item" href="/2023/08/14/%E5%AE%9D%E5%8F%AF%E6%A2%A6%E5%B0%8F%E8%AF%B4/%E5%AE%9D%E5%8F%AF%E6%A2%A6%E5%B0%8F%E8%AF%B45/"><i class="fa  fa-book"></i> 宝可梦小说第五章</a>
            
          
        
          
          
            <a class="list-group-item" href="/2023/08/13/%E5%AE%9D%E5%8F%AF%E6%A2%A6%E5%B0%8F%E8%AF%B4/%E5%AE%9D%E5%8F%AF%E6%A2%A6%E5%B0%8F%E8%AF%B44/"><i class="fa  fa-book"></i> 宝可梦小说第四章</a>
            
          
        
          
          
            <a class="list-group-item" href="/2023/08/10/%E5%AE%9D%E5%8F%AF%E6%A2%A6%E5%B0%8F%E8%AF%B4/%E5%AE%9D%E5%8F%AF%E6%A2%A6%E5%B0%8F%E8%AF%B43/"><i class="fa  fa-book"></i> 宝可梦小说第三章</a>
            
          
        
          
          
            <a class="list-group-item" href="/2023/08/09/%E5%AE%9D%E5%8F%AF%E6%A2%A6%E5%B0%8F%E8%AF%B4/%E5%AE%9D%E5%8F%AF%E6%A2%A6%E5%B0%8F%E8%AF%B42/"><i class="fa  fa-book"></i> 宝可梦小说第二章</a>
            
          
        
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
      </ul>
  </div>
  </aside>

            
    </div>
</section>
        
        </div>
    </div>
</div>
<footer>
    <div id="footer"  >
        <div class="container">
            <div class="row">
                <div class="col-md-6 col-md-offset-3 footer-list text-center">
                    <ul class="kratos-social-icons">
                        
                        
                        
                        
                        
                        
                        
                        
                        
                    </ul>
                    <ul class="kratos-copyright">
                        <div>
                            <li>&copy; 2025 舛羽的笔记小站 版权所有.</li>
                            <li>本站已运行<span id="span_dt">Loading...</span></li>
                        </div>
                        <div>
                            <li>Theme <a href="https://github.com/Candinya/Kratos-Rebirth" target="_blank">Kratos:Rebirth</a></li>
                            <li>Site built with&nbsp;<i class="fa fa-heart throb" style="color:#d43f57"></i>&nbsp;by cy.</li>
                        </div>
                        <div>
                            <li>Powered by <a href="https://hexo.io" target="_blank" rel="nofollow">Hexo</a></li>
                            <li>Hosted on <a href="https://github.io" target="_blank">Github Pages</a></li>
                        </div>
                        <div>
                            
                            
                        </div>
                    </ul>
                </div>
            </div>
        </div>
        <div class="kr-tool text-center">
            <div class="tool">
                
                    <div class="box search-box">
                        <a href="/search/">
                            <span class="fa fa-search"></span>
                        </a>
                    </div>
                
                
                    <div class="box theme-box" id="darkmode-switch">
                        <span class="fa fa-adjust"></span>
                    </div>
                
                
            </div>
            <div class="box gotop-box">
                <span class="fa fa-chevron-up"></span>
            </div>
        </div>
    </div>
</footer>
</div>
</div>

        <script defer src="/vendors/bootstrap@3.3.4/dist/js/bootstrap.min.js"></script>
<script defer src="/vendors/nprogress@0.2.0/nprogress.js"></script>
<script>
    if (!window.kr) {
        window.kr = {};
    }
    window.kr.notMobile = (!(navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)));
    window.kr.siteRoot = "/";
</script>


    <script async src="/js/candy.min.js"></script>




    <script defer src="/vendors/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js"></script>

<script defer src="/vendors/clipboard@2.0.6/dist/clipboard.min.js"></script>
<script defer src="/js/kratosr.min.js"></script>
<script defer src="/js/pjax.min.js"></script>



<!-- Extra support for third-party plguins  -->


    <script type="text/x-mathjax-config">
    MathJax.Hub.Config({
        tex2jax: {
            inlineMath: [ ["$","$"], ["\\(","\\)"] ],
            skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code'],
            processEscapes: true
        }
    });
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax();
        for (var i = 0; i < all.length; ++i)
            all[i].SourceElement().parentNode.className += ' has-jax';
    });
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-MML-AM_CHTML
"></script>
</body>
</html>